package braden.cis.uab.edu.automaton;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class DrawingThread extends Thread {
    private SurfaceHolder surfaceHolder;
    private AbstractDrawingPanel panel;
    private boolean run = false;

    public DrawingThread(SurfaceHolder surfaceHolder, AbstractDrawingPanel drawingPanel) {
        this.surfaceHolder = surfaceHolder;
        this.panel = drawingPanel;
    }

    public void setRunning(boolean run) {
        this.run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return surfaceHolder;
    }

    @Override
    public void run() {
    	Canvas canvas;
        while (run) {
            canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                	try {
                		panel.onDraw(canvas);
                	}catch(Exception e){}
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}
